<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-02-24
 * Time: 12:15
 */

namespace app\mall\service;

use app\model\Company;
use app\model\Goods;
use app\model\GoodsLike;
use app\model\Order;
use app\tools\MIniprogram;

class GoodsService extends BaseService{

    function lists($where, $pageSize, $customer_id)
    {
        $code = db('customer')->where(['id'=> $customer_id])->value('code');
        $where['sale'] = 1;
        $where['num'] = ['>', 0];
        $where2 = "is_time = 0 or is_time = 1 and end_time > '".date('Y-m-d H:i:s')."'";
        $list = Goods::where($where)->where($where2)->field('goods_id, name, cover, is_limit, old_price, price, tiger_tee, is_time, end_time, add_money, add_tiger')->order('sort desc, goods_id desc')->paginate($pageSize)->each(function (&$item) use ($code){
            $item['code'] = $code;
            return $item;
        });
        return $list;
    }

    function lists2($where, $customer_id)
    {
        $category = db('category_index')->where(['status'=> 1])->select();
        $where['sale'] = 1;
        $where['num'] = ['>', 0];

        $code = db('customer')->where(['id'=> $customer_id])->value('code');
        $where2 = " (is_time = 0 or is_time = 1 and end_time > '".date('Y-m-d H:i:s')."') ";
        foreach ($category as &$v)
        {
            if($v['num'] == -1)
            {
                $v['num'] = 100000;
            }
            $where3 = $where2." and (index_id = {$v['id']} or index_id like '%,{$v['id']},%' or index_id like '{$v['id']},%' or index_id like '%,{$v['id']}')";
            $list = Goods::where($where)->where($where3)->field('goods_id, name, cover, is_limit, old_price, price, tiger_tee, is_time, end_time, add_money, add_tiger, sale_num')->order('sort desc, goods_id desc')->limit($v['num'])->select();
            $v['list'] = $list;
            $v['code'] = $code;
        }
        return $category;
    }

    function lists3($param, $pageSize, $customer_id)
    {
        $category = db('category_index')->where(['id'=> $param['category_id']])->find();
        $where['sale'] = 1;
        $where['num'] = ['>', 0];

        $code = db('customer')->where(['id'=> $customer_id])->value('code');
        $where2 = " (is_time = 0 or is_time = 1 and end_time > '".date('Y-m-d H:i:s')."') ";
        $where2 .= " and (index_id = {$category['id']} or index_id like '%,{$category['id']},%' or index_id like '{$category['id']},%' or index_id like '%,{$category['id']}')";

        $list = Goods::where($where)->where($where2)->field('goods_id, name, cover, is_limit, old_price, price, tiger_tee, is_time, end_time, add_money, add_tiger, sale_num')->order('sort desc, goods_id desc')->paginate($pageSize);
        $category['list'] = $list;
        $category['code'] = $code;

        return $category;
    }

    function detail($where, $customer_id)
    {
        $data = Goods::where($where)->field('share, images, name, price, tiger_tee, old_price, num, address, sponsor, details, wx_title, wx_tel, wx_content, wx_num, is_time, end_time, company_id, lat, lng, add_money, add_tiger, like, business_hours, business_tel, uid')->find();
        $data['images'] = explode(',', $data['images']);
        $config = db('my')->field('wx_gzh, icon')->find();
        $data['wx_gzh'] = $config['wx_gzh'];
        $data['icon'] = Company::where(['id'=> $data['uid']])->value('cover');
        $code = db('customer')->where(['id'=> $customer_id])->value('code');
        $data['code'] = $code;
        $data['app_img'] = config('host').MIniprogram::createCodeImg3("packageA/pages/luckyDraw/exchange/exchange", http_build_query([
                'goods_id'=> $where['goods_id'],
            ]));

        $count = GoodsLike::where(['goods_id'=> $where['goods_id'], 'customer_id'=> $customer_id])->count();
        if(!$count)
        {
            GoodsLike::insert([
                'goods_id'=> $where['goods_id'],
                'customer_id'=> $customer_id
            ]);
            Goods::where($where)->setInc('like');
        }

        return $data;
    }

    function recommend($param, $pageSize, $customer_id)
    {
        $code = db('customer')->where(['id'=> $customer_id])->value('code');
        $where['sale'] = 1;
        $where['num'] = ['>', 0];
        $where['is_recommend'] = 1;
        if($param['goods_id']) $where['goods_id'] = ['<>', $param['goods_id']];
        $where2 = "is_time = 0 or is_time = 1 and end_time > '".date('Y-m-d H:i:s')."'";
        $list = Goods::where($where)->where($where2)->field('goods_id, name, cover, is_limit, old_price, price, tiger_tee, is_time, end_time,  add_money, add_tiger, sale_num')->order('sort desc, goods_id desc')->paginate($pageSize)->each(function (&$item) use ($code){
            $item['code'] = $code;
            return $item;
        });
        return $list;
    }

    function customerList($param)
    {
        $data = Order::alias('o')
            ->join('customer c', 'o.customer_id = c.id')
            ->where([
                'o.goods_id'=> $param['goods_id'],
                'o.status'=> ['>', 10]
            ])
            ->field('c.avatar_url, c.nickname')
            ->order('o.id desc')
            ->select();

        return $data;
    }
}